package com.example.test13_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = (TextView) findViewById(R.id.show);
        registerForContextMenu(tv);//为文本框注册上下文菜单

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        MenuInflater inflator = new MenuInflater(this);//实例化一个MenuInfLater对象
        inflator.inflate(R.menu.menus, menu);//解析菜单文件
        menu.setHeaderIcon(R.drawable.ic_launcher_background);//为菜单头设置图标
        menu.setHeaderTitle("请选择");//为菜单头设置标题
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        Toast.makeText(MainActivity.this,item.getTitle(),Toast.LENGTH_SHORT).show();
        return super.onContextItemSelected(item) ;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = new MenuInflater(this);//实例化一个MenuInfLater对象
        inflater.inflate(R.menu.menus, menu);//解析菜单文件
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
        return super.onOptionsItemSelected(item);
    }

}